﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using WinnerCMS.PlugIn.Scheduler;
using WinnerCMS.BLL;
namespace WinnerCMS.HttpModule
{
    public class Task : IHttpModule
    {
        static Timer _eventTimer;

        /// <summary>
        /// 实现接口的Init方法
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            if (_eventTimer == null)
            {
                SchedulerLogs.WriteLog("启动任务调度");
                CMSConfig config = CMSConfig.Instance();
                _eventTimer = new Timer(WorkCallback, context.Context, 60000, config.Scheduler.TimerMinutesInterval * 60000);
            }
            else
            {
                SchedulerLogs.WriteLog("激活任务调度");
            }
        }

        private void WorkCallback(object parameterized)
        {
            if (parameterized == null)
            {
                throw new ArgumentNullException("parameterized");
            }
            try
            {
                CMSConfig config = CMSConfig.Instance();
                if (config.Scheduler.Enabled)
                    SchedulerHelper.Execute(parameterized);
            }
            catch
            {
                SchedulerLogs.WriteLog("任务调度发生未处理异常.");
            }
        }

        /// <summary>
        /// 实现接口的Dispose方法
        /// </summary>
        public void Dispose()
        {
            SchedulerLogs.WriteLog("Dispose.");
        }
    }
}
